﻿@using Orchard.Email.Models
@using System.Net.Mail
@model Orchard.Email.Models.SmtpSettingsPart
@{
    var smtpClient = new SmtpClient();
}
<fieldset>
    <legend>@T("Email")</legend>
    <div>
        <label for="@Html.FieldIdFor(m => m.FromAddress)">@T("Sender email address")</label>
        @Html.TextBoxFor(m => m.FromAddress, new { @class = "text medium", placeholder = Model.AddressPlaceholder })
        @Html.ValidationMessage("FromAddress", "*")
        <span class="hint">@T("The default email address to use as a sender.")</span>
    </div>
    <div>
        <label for="@Html.FieldIdFor(m => m.FromName)">@T("Sender name")</label>
        @Html.TextBoxFor(m => m.FromName, new { @class = "text medium" })
        <span class="hint">@T("The default value to use as a sender name.")</span>
    </div>
    <div>
        <label for="@Html.FieldIdFor(m => m.ReplyTo)">@T("Reply to address")</label>
        @Html.TextBoxFor(m => m.ReplyTo, new { @class = "text medium" })
        <span class="hint">@T("The default email address to use for reply to")</span>
    </div>
    <div>
        <label for="@Html.FieldIdFor(m => m.Host)">@T("Host name")</label>
        @Html.TextBoxFor(m => m.Host, new { placeholder = smtpClient.Host, @class = "text medium" })
        @Html.ValidationMessage("Host", "*")
        <span class="hint">@T("The SMTP server domain, e.g. <i>smtp.mailprovider.com</i>.")</span>
    </div>
    <div>
        <label for="@Html.FieldIdFor(m => m.Port)">@T("Port number")</label>
        @Html.TextBoxFor(m => m.Port, new { type = "number", placeholder = smtpClient.Port, min = 1, max = 65535 })
        @Html.ValidationMessage("Port", "*")
        <span class="hint">@T("The SMTP server port, usually 25.")</span>
    </div>
    <div>
        <label for="@Html.FieldIdFor(m => m.EncryptionMethod)">@T("Encryption method to use")</label>
        <select for="@Html.FieldIdFor(m => m.EncryptionMethod)"
                id="@Html.FieldIdFor(m => m.EncryptionMethod)"
                name="@Html.FieldNameFor(m => m.EncryptionMethod)"
                disabled="@Model.AutoSelectEncryption">
            @Html.SelectOption(
                Model.EncryptionMethod,
                SmtpEncryptionMethod.None,
                String.Join(String.Empty, T("None").ToString(), " - ", T("Connect to server using insecure connection.").ToString()))
            @Html.SelectOption(
                Model.EncryptionMethod,
                SmtpEncryptionMethod.SslTls,
                String.Join(String.Empty, T("SSL/TLS").ToString(), " - ", T("Connect to server using SSL/TSL secure connection.").ToString()))
            @Html.SelectOption(
                Model.EncryptionMethod,
                SmtpEncryptionMethod.StartTls,
                String.Join(String.Empty, T("STARTTLS").ToString(), " - ", T("Connect to server using insecure connection and upgrade to secure using SSL/TLS.").ToString()))
        </select>
        @Html.ValidationMessage("EncryptionMethod", "*")
        <span class="hint">@T("The encryption method used when connecting to mail server.")</span>
    </div>
    <div>
        @Html.EditorFor(m => m.AutoSelectEncryption)
        <label for="@Html.FieldIdFor(m => m.AutoSelectEncryption)" class="forcheckbox">@T("Auto Select Encryption method")</label>
        @Html.ValidationMessage("AutoSelectEncryption", "*")
        <span class="hint">@T("Check to let the system select the encryption method based on port.")</span>
    </div>
    <div>
        @Html.EditorFor(m => m.RequireCredentials)
        <label for="@Html.FieldIdFor(m => m.RequireCredentials)" class="forcheckbox">@T("Require credentials")</label>
        @Html.ValidationMessage("RequireCredentials", "*")
    </div>
    <div data-controllerid="@Html.FieldIdFor(m => m.RequireCredentials)">
        <div>
            <label for="@Html.FieldIdFor(m => m.UserName)">@T("User name")</label>
            @Html.TextBoxFor(m => m.UserName, new { @class = "text medium" })
            @Html.ValidationMessage("UserName", "*")
            <span class="hint">@T("The username for authentication.")</span>
        </div>
        <div>
            <label for="@Html.FieldIdFor(m => m.Password)">@T("Password")</label>
            @Html.TextBoxFor(m => m.Password, new { type = "password", @class = "text medium" })
            @Html.ValidationMessage("Password", "*")
            <span class="hint">@T("The password for authentication.")</span>
        </div>
    </div>
    <div>
        <label for="@Html.FieldIdFor(m => m.ListUnsubscribe)">@T("List-Unsubscribe header")</label>
        @Html.TextBoxFor(m => m.ListUnsubscribe, new { @class = "text medium" })
        <span class="hint">@T("A mailto:link to unsubscribe a user when clicking unsubscribe option")</span>
    </div>
</fieldset>
<fieldset>
    <legend>@T("Test those settings:")</legend>
    <div>
        <label for="emailtestto">@T("To:")</label>
        <input type="text" id="emailtestto" class="large text" />
    </div>
    <div>
        <button type="button" id="emailtestsend" class="button grey">@T("Send")</button>
    </div>
    <div id="emailtesterror" class="message-Error" style="display:none;"></div>
    <div id="emailtestinfo" class="message-Information" style="display:none;"></div>
    @using (Script.Foot())
    {
        <script type="text/javascript">
            $(function () {
                var url = "@Url.Action("TestSettings", "EmailAdmin", new {area = "Orchard.Email"})",
                    error = $("#emailtesterror"),
                    info = $("#emailtestinfo"),
                    fromAddress = $("#@Html.FieldIdFor(m => m.FromAddress)"),
                    fromName = $("#@Html.FieldIdFor(m => m.FromName)"),
                    replyTo = $("#@Html.FieldIdFor(m => m.ReplyTo)"),
                    host = $("#@Html.FieldIdFor(m => m.Host)"),
                    port = $("#@Html.FieldIdFor(m => m.Port)"),
                    encryptionMethod = $("#@Html.FieldIdFor(m => m.EncryptionMethod)"),
                    autoSelectEncryption = $("#@Html.FieldIdFor(m => m.AutoSelectEncryption)"),
                    requireCredentials = $("#@Html.FieldIdFor(m => m.RequireCredentials)"),
                    userName = $("#@Html.FieldIdFor(m => m.UserName)"),
                    password = $("#@Html.FieldIdFor(m => m.Password)"),
                    listUnsubscribe = $("#@Html.FieldIdFor(m => m.ListUnsubscribe)"),
                    to = $("#emailtestto");

                $("#emailtestsend").click(function () {
                    $.post(url, {
                        fromAddress: fromAddress.val(),
                        fromName: fromName.val(),
                        replyTo: replyTo.val(),
                        host: host.val(),
                        port: port.val(),
                        encryptionMethod: encryptionMethod.val(),
                        autoSelectEncryption: autoSelectEncryption.prop("checked"),
                        requireCredentials: requireCredentials.prop("checked"),
                        userName: userName.val(),
                        password: password.val(),
                        listUnsubscribe: listUnsubscribe.val(),
                        to: to.val(),
                        __RequestVerificationToken: to.closest("form").find("input[name=__RequestVerificationToken]").val()
                    })
                    .fail(function (data) {
                        error.html(data.statusText).show();
                        info.hide();
                    })
                    .done(function (data) {
                        if (data.error) {
                            error.html(data.error).show();
                            info.hide();
                        } else {
                            info.html(data.status).show();
                            error.hide();
                        }
                    });
                });
            });
        </script>
    }
</fieldset>